package cn.superchart.common.utils;

import cn.hutool.extra.spring.SpringUtil;
import cn.superchart.common.base.BaseUserDetails;
import cn.superchart.common.enums.AuthEnum;
import com.alicp.jetcache.Cache;
import com.google.common.collect.Lists;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;

public class CacheContext {

    private static final Cache<String, List<SimpleGrantedAuthority>> authCache = SpringUtil.getBean("userCache");

    private static final Cache<String, BaseUserDetails> userCache = SpringUtil.getBean("userCache");

    public static List<SimpleGrantedAuthority> gutAuthCache(String userId){
        List<SimpleGrantedAuthority> grantedAuthorities = authCache.get(AuthEnum.SC_AUTHORITIES_PERM_KEY.getCode()+userId);
        return Checker.BeNotEmpty(grantedAuthorities)?grantedAuthorities: Lists.newArrayList();
    }

    public static void putAuthCache(String userId, List<SimpleGrantedAuthority> grantedAuthorities) {
        authCache.put(AuthEnum.SC_AUTHORITIES_PERM_KEY.getCode()+userId,grantedAuthorities);
    }

    public static void removeAuthCache(String userId) {
        String key = AuthEnum.SC_AUTHORITIES_PERM_KEY.getCode()+userId;
        authCache.remove(key);
    }


    public static BaseUserDetails gutUserCache(String userId){
        String key = AuthEnum.SC_AUTH_USER_KEY.getCode()+userId;
        return  userCache.get(key);
    }

    public static void putUserCache(String userId, BaseUserDetails userDetails) {
        String key = AuthEnum.SC_AUTH_USER_KEY.getCode()+userId;
        userCache.put(key,userDetails);
    }

    public static void removeUserCache(String userId) {
        String key = AuthEnum.SC_AUTH_USER_KEY.getCode()+userId;
        userCache.remove(key);
    }

}
